<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<!--#include file="lib/js-google-analytics.asp"-->
<%
function cf(aaTemp,iFlag)
  cf=false
  if not CheckLogin then
    call ShowError(3)
    exit function
  end if
  if iFlag="" then exit function
  if not IsArray(aaTemp) then exit function
  for i=0 to ubound(aaTemp,2)
    if aaTemp(1,i)<iFlag then exit function
  next
  cf=true
end function


if not cf(session("aaPostFlag"),1) then call ShowError(2)
call SimpleRecord("cd","null","查询","成功",session("iId"),session("iClientId"),false)
'获取提交查询的参数
tx_search=trim(SafeRequest("tx_search",0))
wtr=trim(SafeRequest("wtr",1))
bwtr=trim(SafeRequest("bwtr",1))
category=trim(SafeRequest("category",1))
cd_status=trim(SafeRequest("cd_status",1))
fee=trim(SafeRequest("fee",0))
passno=trim(SafeRequest("passno",0))
orderno=trim(SafeRequest("orderno",0))
ksrq=trim(SafeRequest("ksrq",0))
if ksrq<>"" then ksrq=cdate(ksrq)
jsrq=trim(SafeRequest("jsrq",0))
if jsrq<>"" then jsrq=cdate(jsrq)
jsksrq=trim(SafeRequest("jsksrq",0))
if jsksrq<>"" then jsksrq=cdate(jsksrq)
jsjsrq=trim(SafeRequest("jsjsrq",0))
if jsjsrq<>"" then jsjsrq=cdate(jsjsrq)
ytksrq=trim(SafeRequest("ytksrq",0))
if ytksrq<>"" then ytksrq=cdate(ytksrq)
ytjsrq=trim(SafeRequest("ytjsrq",0))
if ytjsrq<>"" then ytjsrq=cdate(ytjsrq)
cd_received=trim(SafeRequest("cd_received",0))
cd_paid=trim(SafeRequest("cd_paid",0))
cd_confirm=trim(SafeRequest("cd_confirm",0))
cd_check=trim(SafeRequest("cd_check",0))
post=SafeRequest("post",1)
detail=SafeRequest("detail",1)
search_count=trim(SafeRequest("search_count",1))
show_rp=trim(SafeRequest("show_rp",0))
cd_synmode=SafeRequest("cd_synmode",1)
cd_source=SafeRequest("cd_source",1)
duplicate=trim(SafeRequest("duplicate",0))
dept=trim(SafeRequest("dept",1))
ic=trim(SafeRequest("ic",0))
input_ic=trim(SafeRequest("input_ic",0))
roleid=trim(SafeRequest("roleid",1))
userid=trim(SafeRequest("userid",1))
edoc=trim(SafeRequest("edoc",0))
entry_type=trim(SafeRequest("entry_type",0))
cd_hidden=trim(SafeRequest("cd_hidden",0))

'组合sql查询字符串
'sql="select * from cdfee where true "
sql="select "
if search_count<>0 then sql=sql&" top "&search_count
sql=sql&" * from cdfee where true "

if tx_search<>"" then
  if len(tx_search)=18 and IsNumeric(tx_search) then
    '如果是18位的报关单号，直接查询。用于扫描枪的使用，提高搜索速度
    sql=sql&" and cd_no='"&tx_search&"'"
  elseif CheckBoxNo(tx_search) then
    set rs_box=Server.CreateObject("ADODB.Recordset")
    sql_box="select cd from cdbox where box='"&tx_search&"'"
    rs_box.open sql_box,conn,1,1
    if not rs_box.eof then
        '如果找到该集装箱号，就组合字段
        box_querystring=rs_box.getstring(,,,",")
    else
        '如果没找到该集装箱号，就用0来查询
        box_querystring="0"
    end if
    rs_box.close
    set rs_box=nothing
    sql=sql&" and id in ("&box_querystring&")"
  else
    sql=sql&" and (cd_no like '%"&tx_search&"%' or cd_company like '%"&tx_search&"%' or cd_voyage like '%"&tx_search&"%' or cd_lading like '%"&tx_search&"%' or cd_verify like '%"&tx_search&"%' or cd_passno like '%"&tx_search&"%' or cd_orderno like '%"&tx_search&"%'  or cd_memo like '%"&tx_search&"%')"
  end if
end if
' 如果是客户登录查询，则只查询该客户所属委托人的报关单
' 并且只显示收
if CheckWtr() then
  show_rp="true"
  sql=sql&" and wtr="&session("iWtr")
else
  if wtr<>0 then sql=sql&" and wtr="&wtr
  if bwtr<>0 then sql=sql&" and bwtr="&bwtr
end if
if category<>0 then sql=sql&" and category="&category
if cd_status<>0 then sql=sql&" and cd_status="&cd_status
if ksrq<>"" and jsrq<>"" then	sql=sql&" and (cd_date>=#"&ksrq&"# and cd_date<=#"&jsrq&"#)"
if jsksrq<>"" and jsjsrq<>"" then	sql=sql&" and (cd_settledate>=#"&jsksrq&"# and cd_settledate<=#"&jsjsrq&"#)"
if ytksrq<>"" and ytjsrq<>"" then	sql=sql&" and (cd_statusdate>=#"&ytksrq&"# and cd_statusdate<=#"&ytjsrq&"#)"
if cd_received<>"" then sql=sql&" and cd_received="&cd_received
if cd_paid<>"" then sql=sql&" and cd_paid="&cd_paid
if cd_confirm<>"" then sql=sql&" and cd_confirm="&cd_confirm
if cd_check<>"" then sql=sql&" and cd_check="&cd_check
if post=0 then
'如果不选择要查询的岗位，则查询该用户所有的有查询权限的岗位
  sSearchPost=""
  for i=0 to ubound(session("aaPostFlag"),2)
    if session("aaPostFlag")(1,i)>=1 and session("aaPostFlag")(0,i)<>0 and session("aaPostFlag")(0,i)<>5 then
      sSearchPost=sSearchPost&session("aaPostFlag")(0,i)&","
    end if
  next
  if len(sSearchPost)>0 then
    sSearchPost=left(sSearchPost,len(sSearchPost)-1)
    sql=sql&" and cd_post in ("&sSearchPost&")"
  else
    sql=sql&" and cd_post=-1"
  end if
else
'如果选择了查询岗位，则只查询该岗位
  sql=sql&" and cd_post="&post
end if
'查询详细费用的类别，判断是否有该类别的费用产生，返回cd
if detail<>0 then
  set rs_detail=Server.CreateObject("ADODB.Recordset")
  sql_detail="select DISTINCT cd from feedetail where feecategory.id="&detail
  rs_detail.open sql_detail,conn,1,1
  if not rs_detail.eof then
    '如果找到该类别的详细费用，就组合字段
    detail_querystring=rs_detail.getstring(,,,",")
  else
    '如果没找到该类别的详细费用，就用0来查询
    detail_querystring="0"
  end if
  rs_detail.close
  set rs_detail=nothing
  sql=sql&" and id in ("&detail_querystring&")"
end if

if fee<>"" then
  if fee then sql=sql&" and not isnull(cd)" else sql=sql&" and isnull(cd)"
end if
if passno<>"" then
  if passno then sql=sql&" and cd_passno<>''" else sql=sql&" and cd_passno=''"
end if
if orderno<>"" then
  if orderno then sql=sql&" and cd_orderno<>''" else sql=sql&" and cd_orderno=''"
end if
if cd_synmode<>0 then sql=sql&" and cd_synmode="&cd_synmode
if cd_source<>0 then sql=sql&" and cd_source="&cd_source

if duplicate<>"" then
    set rs_duplicate=Server.CreateObject("ADODB.Recordset")
    sql_duplicate="select cd_no from cd group by cd_no having count(cd_no)>1"
    rs_duplicate.open sql_duplicate,conn,1,1
    if not rs_duplicate.eof then
        duplicate_querystring=rs_duplicate.getstring(,,,"','")
        duplicate_querystring="'"&duplicate_querystring&"'"
    else
        duplicate_querystring="''"
    end if
    rs_duplicate.close
    set rs_duplicate=nothing
    if duplicate then
        sql=sql&" and cd_no in ("&duplicate_querystring&")"
    else
        sql=sql&" and cd_no not in ("&duplicate_querystring&")"
    end if
end if
if dept<>0 then
    set rs_ic_code=Server.CreateObject("ADODB.Recordset")
    sql_ic_code="select ic_code from bwtric,deptic where deptic.icid=bwtric.id and deptic.deptid="&dept
    rs_ic_code.open sql_ic_code,conn,1,1
    if not rs_ic_code.eof then
        ic_code_querystring=rs_ic_code.getstring(,,,"','")
        ic_code_querystring="'"&ic_code_querystring&"'"
    else
        ic_code_querystring="''"
    end if
    rs_ic_code.close
    set rs_ic_code=nothing
    sql=sql&" and ic_code in ("&ic_code_querystring&")"
end if
if ic<>"" then sql=sql&" and ic_code='"&ic&"'"
if input_ic<>"" then sql=sql&" and input_iccode='"&input_ic&"'"
if roleid<>0 and userid<>0 then
    sql=sql&" and id in (select cdid from cdroleuser where roleid="&roleid&" and userid="&userid&")"
else
    if roleid<>0 then sql=sql&" and id in (select cdid from cdroleuser where roleid="&roleid&")"
    if userid<>0 then sql=sql&" and id in (select cdid from cdroleuser where userid="&userid&")"
end if

if edoc<>"" then 
    if edoc then
        sql=sql&" and id in (select cd from cdedoc)"
    else
        sql=sql&" and id not in (select cd from cdedoc)"
    end if
end if
if entry_type<>"" then
    sql=sql&" and cd_entrytype='"&entry_type&"'"
end if
if cd_hidden<>"" then sql=sql&" and cd_hidden="&cd_hidden



sql=sql&" order by cd_update desc,id desc"
' response.write sql
'response.end
'执行数据查询
set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
If rs.eof Then
  response.write("没有检索到数据!")
  response.end
else
  sql_sum="select sum(cd_receive) as sum_cd_receive,sum(cd_pay) as sum_cd_pay,sum(cd_actreceive) as sum_cd_actreceive,sum(cd_actpay) as sum_cd_actpay,sum(total_receive) as sum_total_receive,sum(total_pay) as sum_total_pay from("&sql&")"
  set rs_sum = Server.CreateObject("ADODB.Recordset")
  rs_sum.open sql_sum,conn,1,1
  sum_cd_receive=rs_sum("sum_cd_receive")
  sum_cd_pay=rs_sum("sum_cd_pay")
  sum_cd_actreceive=rs_sum("sum_cd_actreceive")
  sum_cd_actpay=rs_sum("sum_cd_actpay")
  sum_total_receive=rs_sum("sum_total_receive")
  sum_total_pay=rs_sum("sum_total_pay")
  rs_sum.close
  set rs_sum=nothing
End if

'分页控制
If saferequest("page",0)<>"" Then	page=clng(saferequest("page",0)) Else	page=1
pc=saferequest("pc",1)
If pc="" Then pc=25
rs.pagesize=pc
pagesize=rs.pagesize
pagecount=rs.pagecount
'page=clng(request.QueryString("page"))
if Not IsEmpty(page) then '如果page_num已经初始化...
  if Not IsNumeric(page) then '判断page_num值是否为数字
    page=1
  else
    page = clng(page) '接收page_num并化为数字型赋给page_num变量
  end if
  if page > pagecount then '如果接收的页数大于总页数
    rs.AbsolutePage = pagecount '设置当前显示页等于最后页
  elseif page <= 0 then '如果page小于等于0
    page=1
    rs.AbsolutePage = page '设置当前显示页等于第一页
  else
    rs.AbsolutePage = page '如果大于零,显示当前页等于接收的页数
  end if
  page = rs.AbsolutePage
end if
%>
<table class="full">
  <thead>
    <tr>
      <th scope="col" rowspan="2"><input type="checkbox" id="checkall"></th>
      <th scope="col" rowspan="2">ID</th>
      <th scope="col" rowspan="2">企业名称</th>
      <%if CheckEmployee() then %>
      <%if wtr=0 then%>
      <th scope="col" rowspan="2">委托人</th>
      <%end if%>
      <%if bwtr<>0 then%>
      <th scope="col" rowspan="2">被委托人</th>
      <%end if%>
      <%end if%>
      <%if category<>0 then%>
      <th scope="col" rowspan="2">类型</th>
      <%end if%>
      <th scope="col" rowspan="2">报关单号</th>
      <th scope="col" rowspan="2">申报日期</th>
      <th scope="col" rowspan="2">船名/航次</th>
      <th scope="col" rowspan="2">提单号</th>
<!--
      <th scope="col" rowspan="2">核销单号</th>
-->
      <th scope="col" rowspan="2">箱号</th>
      <%if passno="true" then%>
      <th scope="col" rowspan="2">通关单号</th>
      <%end if%>
      <%if orderno="true" then%>
      <th scope="col" rowspan="2">合同号</th>
      <%end if%>
      <%if not session("bNotFee") then%>
      <th scope="col" colspan="<%if show_rp="" then response.write("2") else response.write("1")%>">报关费</th>
      <th scope="col" colspan="<%if show_rp="" then response.write("2") else response.write("1")%>">合计</th>
      <th scope="col" colspan="<%if show_rp="" then response.write("2") else response.write("1")%>">实际</th>
      <%end if%>
      <%if post=0 then%>
      <th scope="col" rowspan="2">岗位</th>
      <%end if%>
      <th scope="col" rowspan="2">备注</th>
    </tr>
    <%if not session("bNotFee") then%>
    <tr>
      <%if show_rp<>"false" then%>
      <th scope="col">应收</th>
      <%end if%>
      <%if show_rp<>"true" then%>
      <th scope="col">应付</th>
      <%end if%>
      <%if show_rp<>"false" then%>
      <th scope="col">应收</th>
      <%end if%>
      <%if show_rp<>"true" then%>
      <th scope="col">应付</th>
      <%end if%>
      <%if show_rp<>"false" then%>
      <th scope="col">已收</th>
      <%end if%>
      <%if show_rp<>"true" then%>
      <th scope="col">已付</th>
      <%end if%>
    </tr>
    <%end if%>
  </thead>
  <tbody>
<%
  For i=1 To pagesize
    If rs.eof Then Exit for
%>
    <tr class="<%
    if rs("cd_check") and rs("cd_hidden") then 
        response.write("highlight hidden")
    elseif rs("cd_hidden") then 
        response.write("hidden")
    elseif rs("cd_check") then
        response.write("highlight")
    end if
    %>">
      <th class="sub" scope="row"><input type="checkbox" name="checkid" class="checkid" value="<%=rs("id")&"|"&rs("cd_received")&"|"&rs("total_receive")&"|"&rs("cd_paid")&"|"&rs("total_pay")%>" /></th>
      <th class="sub" scope="row"><a href="process.asp?id=<%=rs("id")%>" target="_blank"><%=rs.AbsolutePosition%></a></th>
      <td><a target="_blank" href="htmlfile.asp?id=<%=rs("id")%>&dtype=tz"><%=rs("cd_company")%></a></td>
      <%if CheckEmployee() then %>
      <%if wtr=0 then%>
      <td><%=rs("wtr_name")%></td>
      <%end if%>
      <%if bwtr<>0 then%>
      <td><%=rs("bwtr_name")%></td>
      <%end if%>
      <%end if%>
      <%if category<>0 then%>
      <td><%=rs("category_name")%></td>
      <%end if%>
      <td title="<%=rs("cd_no")%>">
        <%
        cd_no=rs("cd_no")
        if rs("cd_source")<>2 then cd_no=right(cd_no,9)
        %>
        <%if rs("cd_synmode")=1 then%>
            <a target="_blank" href="htmlfile.asp?id=<%=rs("id")%>"><%=cd_no%></a>
        <%else%>
            <%=cd_no%>
        <%end if%>
      </td>
      <td><%=rs("cd_date")%></td>
      <td class="voyage"><%=rs("cd_voyage")%></td>
      <td><%=rs("cd_lading")%></td>
<!--
      <td><%=rs("cd_verify")%></td>
-->
      <td><%
        Set rs_cdbox = Server.CreateObject("ADODB.Recordset")
        sql_box = "SELECT top 1 box FROM cdbox where cd="&rs("id")
        ' response.write(sql_box)
        ' response.end
        rs_cdbox.Open sql_box,conn,1,1
        if not rs_cdbox.eof then response.write(rs_cdbox("box"))
        rs_cdbox.close
        set rs_cdbox=nothing
      %></td>
      <%if passno="true" then%>
      <td><%=rs("cd_passno")%></td>
      <%end if%>
      <%if orderno="true" then%>
      <td><%=rs("cd_orderno")%></td>
      <%end if%>
      <%if not session("bNotFee") then%>
      <%if show_rp<>"false" then%>
      <td align="center"><%=rs("cd_receive")%></td>
      <%end if%>
      <%if show_rp<>"true" then%>
      <td align="center"><%=rs("cd_pay")%></td>
      <%end if%>
      <%if show_rp<>"false" then%>
      <td align="center" <%if rs("cd_received") then response.write("class='highlight'")%>><%if rs("total_receive")<>rs("cd_receive") then response.write("<strong>"&rs("total_receive")&"</strong>") else response.write(rs("total_receive"))%></td>
      <%end if%>
      <%if show_rp<>"true" then%>
      <td align="center" <%if rs("cd_paid") then response.write("class='highlight'")%>><%if rs("total_pay")<>rs("cd_pay") then response.write("<strong>"&rs("total_pay")&"</strong>") else response.write(rs("total_pay"))%></td>
      <%end if%>
      <%if show_rp<>"false" then%>
      <td align="center"><%=rs("cd_actreceive")%></td>
      <%end if%>
      <%if show_rp<>"true" then%>
      <td align="center"><%=rs("cd_actpay")%></td>
      <%end if%>
      <%end if%>
      <%if post=0 then%>
      <td><%=GetPostString(session("aaPostName"),rs("cd_post"))%></td>
      <%end if%>
      <td><%=rs("cd_memo")%></td>
    </tr>
<%
  rs.movenext()
next
If page=pagecount Then
%>
    <%if not session("bNotFee") then%>
    <tr>
      <th class="sub" scope="row">合计</th>
      <th class="sub" scope="row"></th>
      <th class="sub" scope="row"></th>
      <%if CheckEmployee() then %>
      <%if wtr=0 then%>
      <th class="sub" scope="row"></th>
      <%end if%>
      <%if bwtr<>0 then%>
      <th class="sub" scope="row"></th>
      <%end if%>
      <%end if%>
      <%if category<>0 then%>
      <th class="sub" scope="row"></th>
      <%end if%>
      <th class="sub" scope="row"></th>
      <th class="sub" scope="row"></th>
      <th class="sub" scope="row"></th>
      <th class="sub" scope="row"></th>
      <th class="sub" scope="row"></th>
      <%if passno="true" then%>
      <th class="sub" scope="row"></th>
      <%end if%>
      <%if orderno="true" then%>
      <th class="sub" scope="row"></th>
      <%end if%>
      <%if show_rp<>"false" then%>
      <th class="sub" scope="row"><%=sum_cd_receive%></th>
      <%end if%>
      <%if show_rp<>"true" then%>
      <th class="sub" scope="row"><%=sum_cd_pay%></th>
      <%end if%>
      <%if show_rp<>"false" then%>
      <th class="sub" scope="row"><%=sum_total_receive%></th>
      <%end if%>
      <%if show_rp<>"true" then%>
      <th class="sub" scope="row"><%=sum_total_pay%></th>
      <%end if%>
      <%if show_rp<>"false" then%>
      <th class="sub" scope="row"><%=sum_cd_actreceive%></th>
      <%end if%>
      <%if show_rp<>"true" then%>
      <th class="sub" scope="row"><%=sum_cd_actpay%></th>
      <%end if%>
      <%if post=0 then%>
      <th class="sub" scope="row"></th>
      <%end if%>
      <th class="sub" scope="row"></th>
    </tr>
    <%end if%>
<%end if%>
  </tbody>
</table>
<div>
  <span id="pagebar">
    共查询到&nbsp;<%=rs.RecordCount%>&nbsp;条记录&nbsp;
  <%if CheckFlag(session("aaPostFlag"),2) and (not session("bNotFee")) then%>
    <input id="print_search" class="bt_button" type="button" value="打印查询结果" />&nbsp;&nbsp;
  <%end if%>
    <%	If page>1 Then	%>
      [<a href="#" id="1">首页</a>]
      [<a href="#" id="<%=page-1%>">上一页</a>]
    <%
        End If
        If Not (page>=pagecount Or pagecount=0) Then
    %>
      [<a href="#" id="<%=page+1%>">下一页</a>]
      [<a href="#" id="<%=pagecount%>">尾页</a>]
    <%	End If %>
      第 <%=page%> 页 / 共 <%=pagecount%> 页
      转到<input id="page_val" type="text" size="2" maxlength="<%=Int(Log(pagecount))+1%>" value="<%=page%>" />页
      <input id="page_go" class="bt_button" type="button" value="Go" />
  </span>
  <span>
  <%if post=4 and CheckPostFlag(session("aaPostFlag"),4,4) then%>
  <%if cd_received="false" then%>
    <input class="bt_button bt_act" type="button" value="已收" />
  <%end if%>
  <%if cd_paid="false" then%>
    <input class="bt_button bt_act" type="button" value="已付" />
  <%end if%>
  <%end if%>
  <%if post=4 and cd_confirm="false" and CheckPostFlag(session("aaPostFlag"),6,4) then%>
    <input class="bt_button bt_act" type="button" value="确认" />
  <%end if%>
  <%if post=1 and cd_hidden="false" and CheckPostFlag(session("aaPostFlag"),1,4) then%>
    <input class="bt_button bt_act" type="button" value="隐藏" />
  <%end if%>
  <%if post=1 and cd_hidden="true" and CheckPostFlag(session("aaPostFlag"),1,4) then%>
    <input class="bt_button bt_act" type="button" value="显示" />
  <%end if%>
  <%if (cd_status=1 or cd_status=2) and CheckPostFlag(session("aaPostFlag"),5,4) then%>
    <input class="bt_button bt_act" type="button" value="核销" />
  <%end if%>
  <%if CheckEmployee() then%>
      <%if wtr=1 and post=1 and CheckPostFlag(session("aaPostFlag"),2,4) then%>
        <select name="selwtr" id="selwtr">
            <%
            Set rs_wtr = Server.CreateObject("ADODB.Recordset")
            sql = "SELECT * FROM wtr where wtrhidden=false order by wtr"
            rs_wtr.Open sql,conn,1,1
            do while not rs_wtr.eof
            %>
            <option value="<%=rs_wtr("id")%>" <%if rs_wtr("id")=clng(wtr) then response.write("selected='selected'")%>><%=rs_wtr("wtr")%></option>
            <%
              rs_wtr.movenext
            loop
            rs_wtr.close
            set rs_wtr=nothing
            %>
        </select>
        <input class="bt_button bt_act" type="button" value="认领" />
      <%end if%>
  <%end if%>
  </span>
</div>
<%
rs.close
Set rs=nothing
%>

<!--#include file="lib/connclose.asp"-->

